
***Calculate CI Imbens and Manski (2004)

gen assn=.

replace assn=1 if assumption=="BR"
replace assn=2 if assumption=="MTRMTS"

gen coef_lb=.
gen coef_ub=.
 
gen ci_im_lb=.
gen ci_im_ub=.

foreach ass in BR MTRMTS {
	
sum coef if assumption=="`ass'" & bound=="LB"
local lb=r(mean)
sum coef if assumption=="`ass'" & bound=="UB"
local ub=r(mean)

local delta=(`ub'-`lb')

sum stderr if assumption=="`ass'" & bound=="LB"
local se_lb=r(mean)
sum stderr if assumption=="`ass'" & bound=="UB"
local se_ub=r(mean)

local maxse=max(`se_lb',`se_ub')

di `delta'
di `maxse'


preserve 

clear 
set obs 2

gen 	alph=0.95 	in 1
replace alph=2 		in 2
gen 	x1=1 		in 1
replace x1=0 		in 2
gen 	x2=0 		in 1
replace x2=1 		in 2

nl (alph = (normal({C}+(`delta'/`maxse'))-normal(-{C}))*x1 + {F}*x2)

local C=e(b)[1,1]

assert round(0.95,0.01)==round(normal(`C'+(`delta'/`maxse'))-normal(-`C'),0.01) 

restore


local eps=0.05 // so that rcap overlaps with rbar in the figure

replace ci_im_lb=coef-(`C'*stderr) 	if assumption=="`ass'" & bound=="LB"
sum 	coef	 			 		if assumption=="`ass'" & bound=="LB"
replace ci_im_ub=r(mean)+`eps'		if assumption=="`ass'" & bound=="LB"

replace ci_im_ub=coef+(`C'*stderr) 	if assumption=="`ass'" & bound=="UB"
sum 	coef 						if assumption=="`ass'" & bound=="UB"
replace ci_im_lb=r(mean)-`eps'		if assumption=="`ass'" & bound=="UB"

sum 	coef	 			 		if assumption=="`ass'" & bound=="LB"
replace coef_lb=r(mean) 			if assumption=="`ass'"

sum 	coef	 			 		if assumption=="`ass'" & bound=="UB"
replace coef_ub=r(mean)				if assumption=="`ass'"

}


cd "$OUT"
save ${EXP}_bounds.dta, replace

